package com.suhui.task.controller;

import com.alibaba.fastjson.JSONObject;
import com.suhui.task.common.Result;
import com.suhui.task.pojo.User;
import com.suhui.task.service.Get_header;
import com.suhui.task.service.HttpRequest;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@RestController
public class LoginController {

    private final String URL = "http://172.16.249.145:5000";
//    private final String URL = "http://192.168.136.131:3000/rpc/login";
    private String new_token;

    @Resource
    private HttpRequest httpRequest;

    @Resource
    private Get_header get_header;

    /**
     * 登录获取 token
     */
    @PostMapping("/login")
    public Result<?> login(@RequestBody User user) {
        if (user == null) {
            return Result.error("500","邮箱、密码不能为空");
        }
        if (user.getKey() == null) {
            return Result.error("500","邮箱不能为空");
        }
        if (user.getSecret() == null) {
            return Result.error("500","密码不能为空");
        }
        Map<String,Object> param = new HashMap<>();
        param.put("key",user.getKey());
//        param.put("email",user.getKey());
        param.put("secret",user.getSecret());
//        param.put("pass",user.getSecret());
        // 请求仅判断邮箱密码是否正确！
        String data_string = httpRequest.sendGet(this.URL,param);
        JSONObject jsonObject = JSONObject.parseObject(data_string);
        if (jsonObject.containsKey("code")) {
            if ("28P01".equals(jsonObject.get("code"))) {
                return Result.error("500","用户名密码错误");
            }
        }
        String token = (String) jsonObject.get("token");
        this.new_token = token;
        // 开启定时任务
        ScheduledExecutorService s = Executors.newScheduledThreadPool(1);
        s.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                Map<String,Object> param = new HashMap<>();
//                param.put("email",user.getKey());
//                param.put("pass",user.getSecret());
                param.put("key",user.getKey());
                param.put("secret",user.getSecret());
                // 定时获取新的token
                String s1 = httpRequest.sendGet("http://172.16.249.145:5000/rpc/login", param);
//                String s1 = httpRequest.sendGet("http://192.168.136.131:3000/rpc/login", param);
                JSONObject jsonObject = JSONObject.parseObject(s1);
                String token = (String) jsonObject.get("token");
                get_header.get_token(token);
            }
        },0,60,TimeUnit.MINUTES);
        user.setToken(this.new_token);
        return Result.success(user);
    }

}
